#include <my_head.h>
sem_t sem1;
sem_t sem2;
sem_t sem3;
void *task_func_1(void *arg){
    while(1){
        sem_wait(&sem2);
        printf("B ");
        fflush(stdout);
        sleep(1);
        sem_post(&sem3);
    }
}

void *task_func_2(void *arg){
    while(1){

        sem_wait(&sem3);
        printf("C ");
        fflush(stdout);
        sleep(1);
        sem_post(&sem1);
    }
}

int main(int argc, char const *argv[])
{
    sem_init(&sem1,0,1);
    sem_init(&sem2,0,0);
    sem_init(&sem3,0,0);

    pthread_t tid1,tid2;
    int ret = 0;
    if(0 != (ret = pthread_create(&tid1,NULL,task_func_1,NULL))){
        printf("pthread_create error [%d] [%s]\n",ret,strerror(ret));
        exit(EXIT_FAILURE);
    }
    if(0 != (ret = pthread_create(&tid2,NULL,task_func_2,NULL))){
        printf("pthread_create error [%d] [%s]\n",ret,strerror(ret));
        exit(EXIT_FAILURE);
    }
    while(1){
        
        sem_wait(&sem1);
        printf("A ");
        fflush(stdout);
        sleep(1);
        sem_post(&sem2);
    }

    sem_destroy(&sem1);
    sem_destroy(&sem2);
    sem_destroy(&sem3);
    return 0;
}
